<!DOCTYPE html>
<html lang="zh_CN" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="utf-8">
    <title>消息中心 Message Center</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, config-scalable=0">
    <link rel="stylesheet" th:href="@{/statics/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/statics/style/admin.css}" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-tab layui-tab-card" lay-filter="message_tabs_filter" style="margin-bottom:10px; ">
                    <ul class="layui-tab-title">
                        <li class="layui-this">收到的消息</li>
                        <li>发送的消息</li>
                    </ul>
                </div>
                <table class="layui-hide" id="message-list-table" lay-filter="message-list-filter"></table>

                <script type="text/html" id="message-list-table-toolbar">
                    <div class="layui-btn-container">
                        <button shiro:hasPermission="cms:message:add" class="layui-btn layui-btn-sm" lay-event="add">
                            <i class="linecons-mail"></i> 发送消息
                        </button>
                        <button shiro:hasPermission="cms:message:remove" class="layui-btn layui-btn-sm" lay-event="batchRemove">
                            批量删除
                        </button>
                    </div>
                </script>
                <blockquote class="layui-elem-quote">
                    <div style="margin-bottom: 10px;">
                        加粗字体为未阅读的消息，已阅读的消息仅会保留<span style="color: #1E9FFF"><b>30天</b></span>，过期会被清理；
                    </div>
                </blockquote>
                </div>
            </div>
        </div>
    </div>
</div>


<script th:src="@{/statics/layui/layui.js}"></script>
<script th:inline="none">
    layui.config({
        base: '/statics/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'element', 'table'], function () {
        var $ = layui.$
            , element = layui.element
            , admin = layui.admin
            , table = layui.table;

        element.on('tab(message_tabs_filter)', function(data){
            //layer.msg('切到到了'+ data.index + '：' + this.innerHTML);
            if(data.index === 0) {
                tableResponse('/api/message/recievebox',0);
            }else if(data.index === 1){
                tableResponse('/api/message/sendbox',1);
            }
        });

        tableResponse('/api/message/recievebox',0);

        function tableResponse (url,idx)
        {
            var tableIns = table.render({
                elem: '#message-list-table'
                , url: url
                , id: 'id'
                , toolbar: idx === 0 ? '#message-list-table-toolbar' : ''
                , cols: [[
                    {type: 'checkbox'}
                    , {
                        field: 'title', title: '标题', templet: function (d) {
                            if (d.isRead === 0) {
                                return '<a lay-event="view" data-id="' + d.id + '"><span class="layui-badge-dot layui-bg-blue"></span> <b>' + d.title + '</b></a>';
                            }
                            return '<a lay-event="view" data-id="' + d.id + '">' + d.title + '</a>';
                        }
                    }
                    , {field: 'fromNickName', width: 180, title: '发件人'}
                    , {field: 'createTime', width: 160, title: '时间'}
                ]]
                , page: true
                , request: {
                    pageName: 'pageNum'
                    , limitName: 'pageSize'
                }
                ,done:function(){
                    if(idx === 1){
                        $(".layui-table-box").find("[data-field='0']").css("display","none");
                    }
                }
            });


            //头工具栏事件
            table.on('toolbar(message-list-filter)', function (obj) {
                var checkStatus = table.checkStatus('id')
                    , data = checkStatus.data;
                switch (obj.event) {
                    case 'batchRemove':
                        var ids = '';
                        $.each(data, function (idx, obj) {
                            ids += obj.id + ',';
                        });
                        console.log(ids);
                        ids = ids.substr(0, ids.length - 1);
                        if (ids.length == 0) {
                            layer.msg('请选择要删除的记录!');
                        } else {
                            layer.confirm('真的删除么', function (index) {
                                $.ajax({
                                    url: '/api/message/batchRemove?ids=' + ids,
                                    method: 'DELETE',
                                    data: data.field,
                                    dataType: 'JSON',
                                    success: function (res) {
                                        if (res.code == 0) {
                                            tableIns.reload({page: {curr: 1}}); //数据刷新
                                            layer.msg('记录已删除!', {icon: 1, time: 1000}, function () {
                                                layer.close(index); //关闭弹层
                                            });
                                        } else {
                                            layer.msg(res.msg);
                                        }
                                    },
                                    error: function (err) {
                                    }
                                });
                            });
                        }
                        break;
                    case 'add':
                        var index = layer.open({
                            type: 2
                            , title: '发送新消息'
                            , content: '/cms/my/message/add'
                            , maxmin: false
                            , area: ['600px', '450px']
                            , btn: ['发送', '关闭']
                            , yes: function (index, layero) {
                                var iframeWindow = window['layui-layer-iframe' + index]
                                    , submitID = 'LAY-message-add-submit'
                                    , submit = layero.find('iframe').contents().find('#' + submitID);

                                //监听提交
                                iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                                    //提交 Ajax 成功后，静态更新表格中的数据
                                    $.ajax({
                                        url: '/api/message/add',
                                        method: 'post',
                                        data: data.field,
                                        dataType: 'JSON',
                                        success: function (res) {
                                            if (res.code == 0) {
                                                tableIns.reload(); //数据刷新
                                                layer.close(index); //关闭弹层
                                            } else {
                                                alert(res.msg);
                                            }
                                        },
                                        error: function (err) {
                                        }
                                    });

                                });
                                submit.trigger('click');
                            }
                        });
                        layer.full(index);
                        break;
                }
                ;
            });

            table.on('tool(message-list-filter)', function (obj) {
                var data = obj.data;
                console.log(data);
                switch (obj.event) {
                    case 'view':
                        var index = layer.open({
                            type: 2
                            , title: '查看消息'
                            , content: '/cms/my/message/info/' + data.id
                            , maxmin: false
                            , btn: ['关闭']
                            , yes: function () {
                                tableIns.reload(); //数据刷新
                                layer.close(index); //关闭弹层
                            }
                        });
                        layer.full(index);
                        break;
                }
            });
        }
    });
</script>
</body>
</html>